package com.papper.common.service.sys;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Sets;
import com.papper.common.bean.sys.SysUser;
import com.papper.common.mapper.sys.SysUserMapper;
import com.papper.common.service.ServiceImpl;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service("sysUserService")
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser> {
	@Resource
	SysLimitService sysLimitService;

	public void matchFamily(SysUser sysUser) {
		SysUser parent = findById(sysUser.getParentId());
		String  family;
		if (parent != null) {
			family = parent.getFamily() + "," + sysUser.getId();
		} else {
			family = "0," + sysUser.getId();
		}

		SysUser upd = new SysUser();
		upd.setId(sysUser.getId());
		upd.setFamily(family);
		update(upd);
		sysUser.setFamily(family);
	}

	public void matchChild(SysUser sysUser) {
		if (sysUser == null || sysUser.getId().intValue() == 0) {
			return;
		}

		// 子机构树维护
		Example example = new Example(SysUser.class);
		example.createCriteria().andEqualTo("parentId", sysUser.getId());
		Integer userChild = count(example);
		// 数据更新
		SysUser upd = new SysUser();
		upd.setId(sysUser.getId());
		upd.setUserChild(userChild == null ? 0 : userChild.intValue());
		update(upd);
		sysUser.setUserChild(userChild == null ? 0 : userChild.intValue());
	}

	public void matchLevel(List<SysUser> lst, String family) {
		if (lst == null || lst.size() <= 0) {
			return;
		}

		for (SysUser sysUser : lst) {
			sysUser.setUserLevel(Splitter.on(',').omitEmptyStrings().splitToList(sysUser.getFamily().replaceAll(family, "")).size());
		}
	}

	public List<SysUser> findByGroupId(Long groupId) {
		Example example = new Example(SysUser.class);
		example.createCriteria().andEqualTo("groupId", groupId);
		return find(example);
	}

	public SysUser findByLoginName(Long systemId, String loginName) {
		Example example = new Example(SysUser.class);
		example.createCriteria().andEqualTo("systemId", systemId).andEqualTo("loginName", loginName);
		example.setOrderByClause("id desc limit 1");
		return findOne(example);
	}

	public List<SysUser> findOwnTree(String family) {
		Example example = new Example(SysUser.class);
		example.createCriteria().andEqualTo("family", family);
		return find(example);
	}

	public List<SysUser> findByFamilys(List<String> familys) {
		Example example = new Example(SysUser.class);
		example.createCriteria().andIn("family", familys);
		return find(example);
	}

	public void updatePassword(Long id, String password) {
		SysUser upd = new SysUser();
		upd.setId(id);
		upd.setLoginPass(password);
		upd.setModifyTime(new Date());
		update(upd);
	}

	public void updateLastIp(Long id, String ip) {
		SysUser upd = new SysUser();
		upd.setId(id);
		upd.setLastIp(ip);
		upd.setModifyTime(new Date());
		update(upd);
	}

	public void deleteUserTree(Long id, String family) {
		// 删除用户
		Example example = new Example(SysUser.class);
		example.createCriteria().andEqualTo("family", family);
		delete(example);
		// 删除用户权限信息
		Set<Long> userids = Sets.newHashSet();
		userids.add(id);
		List<SysUser> lstTree = findOwnTree(family);
		if (lstTree != null && !lstTree.isEmpty()) {
			for (SysUser su : lstTree) {
				userids.add(su.getId());
			}
		}
		sysLimitService.delLimitedByUserIds(Joiner.on(",").skipNulls().join(userids));
	}

	public void updateLimitType(Long id, Integer limitType) {
		SysUser upd = new SysUser();
		upd.setId(id);
		upd.setLimitType(limitType);
		upd.setModifyTime(new Date());
		update(upd);
	}
}